package org.go.smart.log;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Date;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class LoggerServlet extends HttpServlet {
	    
	    public void init(ServletConfig config) throws ServletException {
	    	super.init(config);
	    }

	    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	        doGet(request, response);
	    }

	    /* ------------------------------------------------------------ */
	    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	        String day = request.getParameter("day");
	        if (day == null) {
	        	day = GSLogger.formatDate(new Date());
	        }
	        String logFileContent = GSLogger.readFile("gosmart_" + day + ".log", Charset.defaultCharset());
	    	
	    	response.setContentType("text/html");
	        ServletOutputStream out = response.getOutputStream();
	        out.println(logFileContent);
	        out.flush();
	    }

}
